package com.fzkj.juc.forkjoin.stream;

import com.fzkj.juc.forkjoin.entity.User;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @ description    用StreamSupport 实现任务的拆分
 * @ author yaya
 * @ since 2024/9/5
 */
public class StreamSupportApp {


    public static void main(String[] args) {
        List<User> usrList = new ArrayList<>();
        usrList.add(new User(1, "1", "1"));
        usrList.add(new User(2, "2", "2"));
        usrList.add(new User(3, "3", "3"));
        usrList.add(new User(4, "4", "4"));
        usrList.add(new User(5, "5", "5"));
        // 设置一次处理的数量
        int batchSize = 2;

        List<List<User>> list = IntStream.range(0, usrList.size() / batchSize + 1)
                .mapToObj(i -> usrList.stream().skip(i * batchSize).limit(batchSize).toList())
                .toList();

        list.parallelStream().forEach(li -> {
            System.out.println(Thread.currentThread().getName() + " --->>>" + li);
            List<Integer> list1 = li.stream().map(User::getUserId).toList();
            if (list1.size() == 1) {
                System.out.println("单条数据");
            }
        });
    }
}
